﻿using KJSyncXP.Extensions;
using KJSyncXP.ViewModels;
using KJSyncXP.Views;
using System.Windows;
using System.Windows.Controls;

namespace KJSyncXP
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {

        private Standard _standard;
        private ResultRetrial _resultretrial;
        private Changelog _changelog;
        private CheckPoint _checkPoint;
        private CheckResult _checkResult;
        private SetSettings _settings;
        private UserPage _userPage;


        public MainWindow(Standard standardView, ResultRetrial resultRetrial, Changelog changelog, MainWindowViewModel viewmodel,
            CheckPoint checkPoint, SetSettings setSettings, CheckResult checkResult, UserPage userPage)
        {
            InitializeComponent();
            _standard = standardView;
            _resultretrial = resultRetrial;
            _changelog = changelog;
            _checkPoint = checkPoint;
            _settings = setSettings;
            _checkResult = checkResult;
            _userPage = userPage;
            this.DataContext = viewmodel;

            // 监听登录状态变化，动态更新权限
            AuthenticationManager.Instance.PropertyChanged += (sender, e) =>
            {
                if (e.PropertyName == "LoginName")
                {
                    UpdateTabPermissions();
                }
            };
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            UpdateTabPermissions(); // 初始加载时验证权限
        }

        private void UpdateTabPermissions()
        {
            var authManager = AuthenticationManager.Instance;

            foreach (TabItem tabItem in MainTab.Items)
            {
                if (tabItem.Tag is string permKey && !string.IsNullOrEmpty(permKey))
                {
                    // 调用权限验证方法，设置IsEnabled（true=可用，false=禁用）
                    tabItem.IsEnabled = authManager.authorize(permKey);
                }
                else
                {
                    // 无权限标识的选项卡，默认启用
                    tabItem.IsEnabled = true;
                }
            }

            // 确保选中一个“可用”的选项卡
            SelectFirstEnabledTab();
        }

        /// <summary>
        /// 选中第一个“可用”的选项卡
        /// </summary>
        private void SelectFirstEnabledTab()
        {
            foreach (TabItem tabItem in MainTab.Items)
            {
                if (tabItem.IsEnabled)
                {
                    MainTab.SelectedItem = tabItem;
                    break;
                }
            }
        }


        /// <summary>
        /// Tab 切换时，动态加载对应 UserControl 到中间区域
        /// </summary>
        private void MainTab_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            TabItem selectedTab = MainTab.SelectedItem as TabItem;
            if (selectedTab == null) return;

            UserControl targetControl = null;
            string tabHeader = selectedTab.Header.ToString();

            // 根据 Tab 头部分配对应的 UserControl
            switch (tabHeader)
            {
                case "检测标准查询":
                    targetControl = _standard; 
                    break;
                case "结果重判记录":
                    targetControl = _resultretrial; 
                    break;
                case "标准变更记录":
                    targetControl = _changelog; 
                    break;
                case "检点":
                    targetControl = _checkPoint; 
                    break;
                case "检验结果查询":
                     targetControl = _checkResult; 
                     break;
                case "系统设置":
                    targetControl = _settings;
                    break;
                case "权限设置":
                    targetControl = _userPage;
                    break;
            }

            // 更新中间内容区域
            if (targetControl != null)
            {
                MainContent.Content = targetControl;
            }
        }

    }
}
